package com.example.eeuse.util;

import cn.hutool.core.codec.Base64;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.example.eeuse.model.KibanaBaseReq;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class KibanaUtil {

    public static HttpRequest  createPost(String indexName)
    {
        String url="http://10.81.96.48:5601/api/console/proxy?path=%2F"+indexName+"%2F_search&method=GET";
        //账号
        String username="elastic";
        //密码
        String password="monitor*2020";
        HttpRequest httpRequest=HttpUtil.createPost(url);
        httpRequest.header("kbn-version", "8.5.1");
        httpRequest.header("x-kbn-context", "%7B%22name%22%3A%22dev_tools%22%2C%22url%22%3A%22%2Fapp%2Fdev_tools%22%2C%22type%22%3A%22application%22%2C%22page%22%3A%22console%22%7D");
        //组装密码
        String credentials = username+":"+password;
        String encodeCredentials = Base64.encode(credentials, Charset.forName("utf-8"));
        //设置认证信息
        httpRequest.header("Authorization", "Basic " + encodeCredentials);
        return httpRequest;
    }

    public static KibanaBaseReq createReq(int size, List conditionList)
    {
        KibanaBaseReq kibanaBaseReq =new KibanaBaseReq();
        kibanaBaseReq.setSize(size);
        kibanaBaseReq.setQuery(new KibanaBaseReq.Query(new KibanaBaseReq.Bool(conditionList)));
        return kibanaBaseReq;
    }

    public static<T> Map<String,T> createHashMap(String key,T val)
    {
        HashMap<String,T> hashMap=new HashMap<>(1);
        hashMap.put(key,val);
        return hashMap;
    }
}
